package com.aceitunaproject.server.entity.settlement;

import com.aceitunaproject.server.AceitunaContext;
import com.aceitunaproject.server.ServerConfiguration;
import com.aceitunaproject.server.entity.PeopleContainer;
import com.aceitunaproject.server.entity.unit.Unit;
import com.aceitunaproject.server.map.MapTile;
import com.aceitunaproject.server.materialgoods.GoodProduction;
import com.aceitunaproject.server.player.*;
import com.aceitunaproject.server.settlement.SettlementRefreshTask;
import com.aceitunaproject.server.terrain.TerrainType;

import com.sun.sgs.app.AppContext;
import com.sun.sgs.app.Task;

/**
 * Represents inhabited locations and their characteristics
 * 
 * @author Anthony Peterson (MindWanderer)
 * 
 */
public class Settlement extends PeopleContainer {
	/**
	 * Serial UID for serialization purposes 64 bits from
	 * http://www.famkruithof.net/uuid/uuidgen
	 */
	private static final long serialVersionUID = 0x6a7ff0d0c46a11ddL;

	/**
	 * Creates a new settlement
	 * 
	 * @param id
	 *            the id for this object (serialization)
	 * @param name
	 *            the name of this object (serialization)
	 * @param owner
	 *            the player who ultimately owns the settlement
	 * @param controller
	 *            the player delegated control of this settlement from owner
	 * @param unit
	 *            the unit that is creating this settlement and will be
	 *            disbanded in the process
	 */
	public Settlement(Long id, String name, Player owner, Player controller,
			Unit unit) {
		super(id, name, owner, controller, unit.getMorale(), unit.getPersons(),
				unit.getLoyalties(), unit.getCurrentPosition());

		Task refreshTask = new SettlementRefreshTask(this);
		int duration = AceitunaContext.getTickManager().getTickDuration();

		AppContext.getTaskManager().schedulePeriodicTask(refreshTask,
				duration, duration);
		

	}

	public void refreshPopulation() {
		long neededFood = this.getPersons()
				* ServerConfiguration.getFoodPersonRate();

		MapTile settlementTile = AceitunaContext.getMapManager().getTile(
				this.getCurrentPosition());

		int availableFood = 0;
		for (TerrainType tt : settlementTile.getTerrainTypes().values()) {

			for (GoodProduction gp : tt.getResources()) {
				if (gp.getMaterialGood().isFood()) {
					availableFood += 1 * gp.getBlocksPerTick();
				}
			}
		}

		if (Math.abs(neededFood - availableFood) > ServerConfiguration
				.getSignificantFoodDifference()) {
			AppContext.getDataManager().markForUpdate(this);

			if (neededFood > availableFood) {
				this.setMorale(this.getMorale() - (neededFood - availableFood)
						* ServerConfiguration.getHungerMoraleEffect());
			}
			if (availableFood > neededFood) {
				this.setMorale(this.getMorale() + (availableFood - neededFood)
						* ServerConfiguration.getFoodAbundanceMoraleEffect());

				this.setPersons(this.getPersons()
						+ (availableFood - neededFood)
						* ServerConfiguration.getFoodPopulationEffect());

			}
		}

	}
}